// 引入api
import {reqGetnew,reqGetgroupon,reqGetcountdown,reqGetseckill,reqGetindexgoods,reqCartadd,reqGetcoupon} from "../../request/api"
// 初始值
const initState = {
    // 初始化data
    // 新人专享
    getnew: [],
    // 万人团
    groupon: [],
    // 倒计时
    countdown:[],
    // 秒杀
    seckill:[],
    // 商品信息
    indexgoods:[],
    // 导航标识
    flag:1,
    // 优惠券
    cheap:[],
}

// actionsTypes
const types = {
    // 修改数据 type
    // 新人专享
    CHANGE_GETNEW: "HOME_CHANGE_GETNEW",
    // 万人团
    CHANGE_GROUPON: "HOME_CHANGE_GROUPON",
    // 倒计时
    CHANGE_COUNTDOWN: "HOME_CHANGE_COUNTDOWN",
    // 秒杀
    CHANGE_SECKILL: "HOME_CHANGE_SECKILL",
    // 商品信息
    CHANGE_INDEXGOODS: "HOME_CHANGE_INDEXGOODS",
    // 导航标识
    CHANGE_GLAG: "HOME_CHANGE_GLAG",
    // 优惠券
    CHANGE_CHEAP: "HOME_CHANGE_CHEAP",
}

// reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        // 新人专享
        case types.CHANGE_GETNEW:
            return {
                ...state,
                getnew:action.getnew
            }
        // 万人团
        case types.CHANGE_GROUPON:
            return {
                ...state,
                groupon:action.groupon
            }
        // 倒计时
        case types.CHANGE_COUNTDOWN:
            return {
                ...state,
                countdown:action.countdown
            }
        // 秒杀
        case types.CHANGE_SECKILL:
            return {
                ...state,
                seckill:action.seckill
            }
        // 商品信息
        case types.CHANGE_INDEXGOODS:
            return {
                ...state,
                indexgoods:action.indexgoods
            }
        // 导航标识
        case types.CHANGE_GLAG:
            return {
                ...state,
                flag:action.flag
            }
        // 优惠券
        case types.CHANGE_CHEAP:
            return {
                ...state,
                cheap:action.cheap
            }
        default:
            return state;
    }

}

// actionCreators组件派发的动作
export const actions={
    // 修改user的action
    // 新人专享
    changeGetnew:(getnew)=>({type:types.CHANGE_GETNEW,getnew}),
    // 万人团
    changeGroupon:(groupon)=>({type:types.CHANGE_GROUPON,groupon}),
    // 倒计时
    changeCountdown:(countdown)=>({type:types.CHANGE_COUNTDOWN,countdown}),
    // 秒杀
    changeSeckill:(seckill)=>({type:types.CHANGE_SECKILL,seckill}),
    // 商品信息
    changeIndexgoods:(indexgoods)=>({type:types.CHANGE_INDEXGOODS,indexgoods}),
    // 导航标识
    changeFlag:(flag)=>({type:types.CHANGE_GLAG,flag}),
    // 优惠券
    changeCheap:(cheap)=>({type:types.CHANGE_CHEAP,cheap}),

    
    // 请求(异步操作)
    // 新人专享
    reqGetnew:()=>(dispatch,getState)=>{
        reqGetnew().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeGetnew(res.data.list))
            }
        })
    },
    // 万人团
    reqGetgroupon:()=>(dispatch,getState)=>{
        reqGetgroupon().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeGroupon(res.data.list))
            }
        })
    },
    // 倒计时
    reqGetcountdown:()=>(dispatch,getState)=>{
        reqGetcountdown().then(res=>{
            if (res.data.code === 200) {
                dispatch(actions.changeCountdown(res.data.list))
            }
        })
    },
    // 秒杀
    reqGetseckill:()=>(dispatch,getState)=>{
        reqGetseckill().then(res=>{
            if (res.data.code === 200) {
                dispatch(actions.changeSeckill(res.data.list))
            }
        })
    },
    // 商品信息
    reqGetindexgoods:()=>(dispatch,getState)=>{
        reqGetindexgoods().then(res=>{
            if (res.data.code === 200) {
                dispatch(actions.changeIndexgoods(res.data.list))
            }
        })
    },
    // 优惠券
    reqGetcoupon:()=>(dispatch,getState)=>{
        reqGetcoupon().then(res=>{
            if (res.data.code === 200) {
                dispatch(actions.changeCheap(res.data.list))
            }
        })
    },
    // 加购
    reqCartadd:(id)=>(dispatch,getState)=>{
        reqCartadd({uid:JSON.parse(sessionStorage.getItem("user")).uid,type:1,goodsid:id,num:1})
    },
}

//selectors导出数据 
// 新人专享
export const getGetnew=state=>state.home.getnew;
// 万人团
export const getGroupon=state=>state.home.groupon;
// 倒计时
export const getCountdown=state=>state.home.countdown;
// 秒杀
export const getSeckill=state=>state.home.seckill;
// 商品信息
export const getIndexgoods=state=>state.home.indexgoods;
// 商品信息
export const getFlag=state=>state.home.flag;
// 商品信息
export const getCheap=state=>state.home.cheap;
  
export default reducer